<!--实名认证-->
<template>
	<view class="verified">
		<view class="verified-header">
			<view class="verified-header-item2" v-if="accountType=='劳务企业'||accountType=='工程企业'">
				<text>社会统一信用代码</text>
				<input :disabled="isVerified" type="text" placeholder="请填写" v-model="socialCode" />
			</view>
			<view v-if="accountType=='工人'" style="margin-bottom: 40rpx;">
				<view class="verified-header-item">
					<text>真实姓名</text>
					<input :disabled="isVerified" type="text" placeholder="请填写" v-model="realName" />
				</view>
				<!-- <view class="verified-header-item">
					<text>身份证号</text>
					<input :disabled="isVerified" type="idcard" placeholder="请填写" v-model="idCard" />
				</view> -->
			</view>
			<view v-if="accountType=='班组长'" style="margin-bottom: 40rpx;">
				<view class="verified-header-item">
					<text>班组名称</text>
					<input :disabled="isVerified" type="text" placeholder="请填写" v-model="teamName" />
				</view>
				<!-- <view class="verified-header-item">
					<text>班组类型</text>
					<u-input v-model="type_of_work" input-align="right" placeholder="请选择" type="select" placeholder-style="#808080"
						@click="type_show=true" />
					<u-select v-model="type_show" :list="type_list" @confirm="confirm_type"></u-select>
				</view> -->
				<view class="verified-header-item">
					<text>班组长姓名</text>
					<input :disabled="isVerified" type="text" placeholder="请填写" v-model="foremanName" />
				</view>
			</view>
			<view class="verified-header-item" v-if="accountType=='劳务企业'||accountType=='工程企业'">
				<text>公司名称</text>
				<input :disabled="isVerified" type="text" placeholder="请填写" v-model="companyName" />
			</view>
			<view class="verified-header-item">
				<text>联系电话</text>
				<input :disabled="isVerified" type="number" placeholder="请填写" v-model="mobile" />
				<button v-if="!isVerified" type="primary" :loading="vcodeLoading" class="v-button"
					@click="sendVcode()">{{vcodeText}}</button>
			</view>
			<view class="verified-header-item" v-if="!isVerified">
				<text>验证码</text>
				<input type="text" placeholder="请填写" v-model="vCode" />
			</view>
		</view>
		<!-- <view class="id-info">
			<text class="title">{{isVerified?'身份证信息':'上传身份证信息'}}</text>

			<view class="positive" @click="addPhoto('positive')">
				<image :src="idCardPositive" mode=""></image>
				
				<text> {{isVerified?'身份证正面人像图片':'点击上传身份证正面人像图片'}}</text>
			</view>

			<view class="negative" @click="addPhoto('negative')">
				<image :src="idCardNegative" mode=""></image>
				<text>{{isVerified?'身份证反面国徽图片':'点击上传身份证反面国徽图片'}}</text>
			</view>

			<view class="prompt" v-if="!isVerified">
				<text class="prompt-title">拍摄时，确认身份边框完整、字体清晰、亮度均匀</text>

			</view>

		</view> -->

		<!-- <view class="id-info" style="margin-top: 20rpx;" v-if="accountType=='劳务企业'||accountType=='工程企业'">
			<text class="title">{{isVerified?'工商营业执照':'上传工商营业执照'}}</text>

			<view class="positive" @click="addPhoto('datas')">
				<image class="yyzz" :src="datas" mode=""></image>
				<text>{{isVerified?'工商营业执照':'点击上传工商营业执照'}}</text>
			</view>


		</view> -->
		<button class="submit" v-if="!isVerified" @click="submit">提交审核</button>
	</view>
</template>

<script>
	import {
		sendSms
	} from '@/static/js/api/main.js'
	import {
		pathToBase64,
		base64ToPath
	} from '@/static/js/photoUtils.js'
	import {
		verified,
		verified1,
		getUserInfo
	} from '@/static/js/api/user.js'
	import request from '../../../static/js/request.js'
	export default {
		data() {
			return {
				isVerified: false,
				realName: "",
				idCard: "",
				mobile: "",
				vCode: '',
				//正面
				idCardPositive: require('@/static/image/mine/idcard2.png'),
				// 反面
				idCardNegative: require('@/static/image/mine/idcard1.png'),
				account_type: '',
				vcodeLoading: false,
				vcodeText: '发送验证码',
				partnerModel: {},
				accountType: '',
				choosePhoto1: false,
				choosePhoto2: false,
				chooseDatas: false,
				companyName: '',
				socialCode: '',
				datas: require('@/static/image/mine/yyzz.png'), //营业执照
				foremanName:'',//班组长姓名
				teamName:'',//班组名称
				type_of_work: '',
				type_show: false,
				type_list: [{
						label: '木工'
					},
					{
						label: '砌筑工'
					},
					{
						label: '抹灰工'
					},
					{
						label: '钢筋工'
					},
					{
						label: '油漆工'
					},
					{
						label: '防水工'
					},
					{
						label: '模板工'
					},
					{
						label: '筑路工'
					},
					{
						label: '水暖工'
					},
					{
						label: '混凝土工'
					},
					{
						label: '安装工'
					},
					{
						label: '普通工其他'
					}
				],
			}
		},
		onLoad(option) {
			if (this.isVerified) {
				this.getUserInfo();
			}
			this.accountType = option.accountType;
			console.log(this.accountType)
		},
		onShow() {

		},
		onUnload(){
			// uni.showModal({
			// 	title: '注册未完成，是否确认退出?',
			// 	success: (res) => {
			// 		if (res.confirm) {
			// 			uni.clearStorageSync();
			// 			uni.navigateTo({
			// 				url: "/pages/main/mine"
			// 			})
			// 		}
			// 	}
			// });
			
			// if(JSON.parse(uni.getStorageSync('userInfo')).account_type==''){
			// 	uni.clearStorageSync();
			// }
		},
		methods: {
			// confirm_type(e) {
			// 	this.type_of_work = e[0].label
			// },
			addPhoto(type) {
				let self = this;
				if (!this.isVerified) {
					uni.chooseImage({
						count: 1,
						success(res) {
							uni.compressImage({
								src: res.tempFilePaths[0],
								quality: 50,
								success: res => {
									console.log(res)
									if (type == "positive") {
										self.idCardPositive = res.tempFilePath;
										console.log(self.idCardPositive)
										self.choosePhoto1 = true;
									} else if (type == "negative") {
										self.idCardNegative = res.tempFilePath;
										self.choosePhoto2 = true;
									} else if (type == "datas") {
										self.datas = res.tempFilePath;
										self.chooseDatas = true;
									}
								}
							})
						}
					})
				}

			},
			sendVcode() {
				if (!this.mobile || !this.isPoneAvailable(this.mobile)) {
					uni.showToast({
						icon: 'none',
						title: '请输入正确的手机号'
					})
					return;
				}
				uni.showLoading({
					title: '正在发送验证码'
				})
				sendSms(this.mobile).then(res => {
					uni.hideLoading();
					this.vcodeLoading = true;
					let second = 59;
					this.vcodeText = 60 + 's'
					let timer = setInterval(() => {
						this.vcodeText = second-- + 's'
						if (second == 0) {
							this.vcodeText = '发送验证码';
							clearInterval(timer);
							this.vcodeLoading = false;
						}
					}, 1000)

				}).catch(e => {
					uni.hideLoading();
				})

			},
			isPoneAvailable(poneInput) {
				var myreg = /^1[3|4|5|7|8][0-9]{9}$/;
				if (!myreg.test(poneInput)) {
					return false;
				} else {
					return true;
				}
			},
			IDCard(value) {
				let reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
				return reg.test(value)
			},
			async submit() {
				// console.log(pathToBase64(this.datas));
				if (!this.realName&&this.accountType == '工人') {
					uni.showToast({
						icon: 'none',
						title: '请输入姓名'
					})
					return
				}
				if (!this.mobile) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机号码'
					})
					return
				}
				if (!this.vCode) {
					uni.showToast({
						icon: 'none',
						title: '请输入手机验证码'
					})
					return
				}
				// if (!this.companyName && this.accountType == '劳务企业') {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入公司名称'
				// 	})
				// 	return
				// }
				// if (!this.socialCode && this.accountType == '劳务企业') {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入社会统一信用代码'
				// 	})
				// 	return
				// }
				
				if (!this.teamName&&this.accountType == '班组长') {
					uni.showToast({
						icon: 'none',
						title: '请输入班组名称'
					})
					return
				}
				// if (!this.type_of_work&&this.accountType == '班组长') {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入班组类型'
				// 	})
				// 	return
				// }
				if (!this.foremanName&&this.accountType == '班组长') {
					uni.showToast({
						icon: 'none',
						title: '请输入班组长姓名'
					})
					return
				}
				// if (!this.datas && this.accountType == '劳务企业管理人员') {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请上传工商营业执造'
				// 	})
				// 	return
				// }
				// if (!this.choosePhoto2) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请上传身份证照片'
				// 	})
				// 	return
				// }
				// if (!this.choosePhoto1) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请上传身份证照片'
				// 	})
				// 	return
				// }

				// if (!this.chooseDatas && this.accountType == '劳务企业') {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请上传营业执照'
				// 	})
				// 	return
				// }
				// if (!this.IDCard(this.idCard)) {
				// 	uni.showToast({
				// 		icon: 'none',
				// 		title: '请输入正确的身份证号码'
				// 	})
				// 	return
				// }
				uni.showLoading({
					title: '正在加载'
				})
				let res1 = '';
				let res2 = '';
				let res3 = '';
				if (this.choosePhoto1) {
					res1 = await pathToBase64(this.idCardPositive);
					// console.log(res1,3333)
					// res1 = this.idCardPositive;
				}
				if (this.choosePhoto2) {
					res2 = await pathToBase64(this.idCardNegative);
					// res2 = this.idCardNegative;
				}

				// if (this.chooseDatas) {

				// 	res3 = await pathToBase64(this.datas);
				// 	// res3 = this.datas;
				// }
				let groups_id = '';
				if (this.accountType == '工程企业') {
					groups_id = 'Amos_jhj.jhj_enterprises_admin'
				} else if (this.accountType == '工人') {
					groups_id = 'Amos_jhj.jhj_worker'
				} else if (this.accountType == '班组长') {
					groups_id = 'Amos_jhj.jhj_leader'
				} else if (this.accountType == '劳务企业') {
					groups_id = 'Amos_jhj.jhj_personal_service'
				}
				let uid = JSON.parse(uni.getStorageSync('userInfo')).uid
				var name;
				if(this.realName){
					name = this.realName
				}else{
					name = this.foremanName
				}
				// let data = {
				// 	uid: uid,
				// 	// name: this.realName,
				// 	name: name,
				// 	// idcard: this.idCard,
				// 	mobile: this.mobile,
				// 	verifyCode: this.vCode,
				// 	// idcard_file_a: res1 ? res1.replace('data:image/png;base64,', '') : '',
				// 	// idcard_file_b: res2 ? res2.replace('data:image/png;base64,', '') : '',
				// 	account_type: this.accountType,
				// 	groups_id,
				// 	// company_name: this.companyName,
				// 	// socialCode: this.socialCode,
				// 	// business_license: res3 ? res3.replace('data:image/png;base64,', '') : '',
				// 	team_name:this.teamName,
				// 	work_type:this.type_of_work,
				// 	// team_leader_name:this.foremanName
				// }
				let data = {
					uid: uid,
					name: name,
					mobile: this.mobile,
					verifyCode: this.vCode,
					account_type: this.accountType,
					groups_id,
					team_name:this.teamName,
					web:'123'
				}
				let resp = await verified(data)
				uni.showToast({
					title: '注册成功'
				})
				uni.hideLoading();
				let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
				userInfo.is_confirm = resp.data.is_confirm
				userInfo.team_name = resp.data.team_name
				userInfo.avatarUrl = resp.data.image_1920
				userInfo.account_type = resp.data.account_type;
				userInfo.name = resp.data.name;
				userInfo.team_id = resp.data.team_id;
				userInfo.location_company_id = resp.data.location_company_id
				userInfo.location_company_id_name = resp.data.location_company_id_name
				uni.setStorageSync('userInfo', JSON.stringify(userInfo));
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/main/mine'
					})
				}, 1000)
			},

			getUserInfo() {
				getUserInfo().then(res => {
					console.log(res, 2222222)
					let _res = res[0];
					this.realName = _res.name;
					this.idCard = _res.idcard;
					this.mobile = _res.mobile;
					//正面
					this.idCardPositive = 'data:image/png;base64,' + _res.idcard_file_a;
					// 反面
					this.idCardNegative = 'data:image/png;base64,' + _res.idcard_file_b;
				})
			},

			// uploadFile(filePath, id, flag) {
			// 	let userInfo = JSON.parse(uni.getStorageSync('userInfo'));
			// 	let self = this;
			// 	uni.uploadFile({
			// 		url: request.getBaseUrl() + '/api/v1/upload_img/9', // 后台服务URL
			// 		filePath: filePath, // 微信返回的临时图片地址
			// 		name: 'upload_files',
			// 		header: {
			// 			"Content-Type": "multipart/form-data"
			// 		},
			// 		formData: { // 其他参数
			// 			access_token: userInfo.access_token,
			// 			model: 'res.partner', //当前对象（表名）
			// 			uid: userInfo.uid, //当前用户 uid，      
			// 			partner_id: userInfo.partner_id, //当前用户partner_id，
			// 			res_id: id,
			// 			flag: flag,
			// 			path:'zj'
			// 		},

			// 	});
			// },
		}
	}
</script>

<style lang="less" scoped>
	.verified {
		background: #F4F5F9;
		display: flex;
		flex-direction: column;
	}

	.title {
		color: #333333;
		font-size: 36rpx;
		font-weight: 500;
	}

	.verified-header {
		padding: 40rpx 32rpx;
		background: #fff;

		// flex-direction: column;
		margin-bottom: 20rpx;

		.verified-header-item2 {
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
			line-height: 50rpx;
			display: flex;
			align-items: center;
			margin-bottom: 40rpx;
			position: relative;

			.v-button {
				// position: absolute;
				font-size: 12px;
				// right: 0rpx;
				background-color: #B8212B;
				color: #fff;
				height: 70rpx;
				line-height: 70rpx;
				z-index: 10;
			}

			&>text {
				// flex: 1;
				// font-size: 22rpx;
				width: 180rpx;
			}

			input {
				// width: 440rpx;
				flex: 1;
				height: 44rpx;
				background: #EFF0F1;
				border-radius: 8rpx;
				padding: 14rpx 20rpx;
			}
		}

		.verified-header-item:last-child {
			margin-bottom: 0rpx;
		}

		.verified-header-item {
			font-size: 36rpx;
			font-weight: 500;
			color: #333333;
			line-height: 50rpx;
			display: flex;
			align-items: center;
			margin-bottom: 40rpx;
			position: relative;

			.v-button {
				// position: absolute;
				font-size: 12px;
				// right: 0rpx;
				background-color: #B8212B;
				color: #fff;
				height: 70rpx;
				line-height: 70rpx;
				z-index: 10;
			}

			&>text {
				// flex: 1;
				width: 180rpx;
			}

			input {
				// width: 440rpx;
				flex: 1;
				height: 44rpx;
				background: #EFF0F1;
				border-radius: 8rpx;
				padding: 14rpx 20rpx;
			}
		}

	}

	.id-info {
		display: flex;
		flex-direction: column;
		background: #fff;
		padding: 32rpx 34rpx;

		.positive {
			display: flex;
			flex-direction: column;
			margin-top: 34rpx;
			align-items: center;

			.yyzz {
				width: 484rpx;
				height: 484rpx;
			}

			image {
				width: 484rpx;
				height: 302rpx;
			}

			text {
				margin-top: 10rpx;
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
			}
		}

		.negative {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 34rpx;

			image {

				width: 484rpx;
				height: 302rpx;
			}

			text {
				margin-top: 10rpx;
				font-weight: 500;
				font-size: 30rpx;
				color: #333333;
			}
		}

		.prompt {
			display: flex;
			flex-direction: column;
			align-items: center;
			margin-top: 48rpx;

			.prompt-title {
				font-size: 28rpx;
				font-weight: 500;
				color: #333333;
				margin-bottom: 12rpx;
			}

			.prompt-img {
				display: flex;
				align-items: center;

				.prompt-img-item:last-child {
					margin-right: 0rpx;
				}

				.prompt-img-item {
					// flex: 1;
					display: flex;
					flex-direction: column;
					align-items: center;
					margin-right: 48rpx;

					image {
						width: 126rpx;
						height: 116rpx;

					}

					text {
						font-weight: 400;
						font-size: 26rpx;
						color: #A4A4A6;
					}
				}
			}
		}
	}

	.submit {
		font-weight: 500;
		font-size: 32rpx;
		height: 80rpx;
		background: #B8212B;
		border-radius: 8rpx;
		display: flex;
		color: #FFFFFF;
		align-items: center;
		justify-content: center;
		margin: 40rpx 34rpx 40rpx;
	}
	/deep/u-input{
		    flex: 1;
		    background: #EFF0F1;
		    border-radius: 8rpx;
		    padding: 0 20rpx;
				.u-input__input{
					font-size: 36rpx;
					text-align: left;
				}
	}
</style>
